/**
 * 
 */
package at.ac.lbg.media.vis.framework.service;


import java.math.BigDecimal;
import java.math.MathContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import no.uib.cipr.matrix.Matrix;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import at.ac.lbg.media.vis.framework.domain.ArtworkDO;
import at.ac.lbg.media.vis.framework.domain.ArtworkDetailsDO;
import at.ac.lbg.media.vis.framework.domain.CategoryDO;
import at.ac.lbg.media.vis.framework.model.Artwork;
import at.ac.lbg.media.vis.framework.model.ArtworkDetails;
import at.ac.lbg.media.vis.framework.model.ThemeLandscapeHexboardDataItem;
import at.ac.lbg.media.vis.framework.persistence.IArtworkDAO;

/**
 * @author Evelyn Münster
 *
 */
public class ArtworkService implements IArtworkService {
	
	private IArtworkDAO artworkDAO;
	private ICategoryService categoryService;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public ArtworkService() {}

	@Override
	public List<ArtworkDO> getAllArtworks() {
		return artworkDAO.getAllArtworks();
	}
	
	@Override
    public List<ArtworkDO> getAllArtworks(int sectionId) {
        List<ArtworkDO> artworks = artworkDAO.getAllArtworks();
        for (ArtworkDO aw : artworks) {
            aw.setCategories(categoryService.filter(aw.getCategories(), sectionId));
        }
        return artworks;
    }
	
    @Override
    public ArtworkDetailsDO getArtworkDetails(int artworkId) {
        return artworkDAO.getArtworkDetails(new Integer(artworkId).longValue());
    }
	
	@Override
	public void copyValues(ArtworkDetails awd, ArtworkDetailsDO awdDO) {
	    awd.setId(awdDO.getId());
	    awd.setTitle(awdDO.getTitle());
	    awd.setYear(awdDO.getYear());
	    awd.setNica(awdDO.getNica());
	    awd.setAuthors(awdDO.getAuthors());
	    awd.setIntroductionG(awdDO.getIntroductionG());
	    awd.setIntroductionE(awdDO.getIntroductionE());
	    awd.setImgFileName(awdDO.getImgFileName());
	    List<CategoryDO> categories = awdDO.getCategories();
        awd.setCategories(categoryService.translate(categories));
	}
	
    /**
     * @param artworks
     * @param artworkDOs
     */
	@Override
    public void copyValues(List<Artwork> artworks, List<ArtworkDO> artworkDOs, Matrix coordinates) {
        // stack holds one entry for each stack of artworks, i.e. artworks which
        // coordinates match. key: x+y coordintes as String, like "3.87620.2983"
        // value: number of artworks in the stack
        Map<String, Integer> stack = new HashMap<String, Integer>();
     // copy values from ArtworkDO and the corresponding coordinates
        // to Artwork
        for (int i = 0; i < artworkDOs.size(); i++) {
            ArtworkDO aDO = artworkDOs.get(i);
            Artwork a = new Artwork();
            a.setId(aDO.getId());
            a.setTitle(aDO.getTitle());
            a.setYear(aDO.getYear());
            a.setNica(aDO.getNica());
            List<CategoryDO> categories = aDO.getCategories();
            a.setCategories(categoryService.translate(categories));
            BigDecimal x = new BigDecimal(coordinates.get(i, 0));
            BigDecimal y = new BigDecimal(coordinates.get(i, 1));
            MathContext mc = new MathContext(2);
            a.setX(x.round(mc).doubleValue());
            a.setY(y.round(mc).doubleValue());
            String key = Double.toString(a.getX()) + "#" + Double.toString(a.getY());
            Integer stackCount = stack.get(key);
            if (stackCount != null) {
                stack.put(key , ++stackCount);
            } else {
                stack.put(key , 1);
            }
            //if (a.getCategories().size() > 0) {
                artworks.add(a);
            //}
        }
        
        for (Artwork a : artworks) {
            String key = Double.toString(a.getX()) + "#" + Double.toString(a.getY());
            a.setStackCount(stack.get(key));
        }
    }
	
	/*
	 * Copies all fields from domainobject to model.
	 * Does not prepare stacks.
	 * Does not insert coordinates.
	 * Used for Hexboard version.
	 */
	@Override
    public void copyValues(List<Artwork> artworks, List<ArtworkDO> artworkDOs) {

        for (ArtworkDO aDO : artworkDOs) {
            Artwork a = new Artwork();
            a.setId(aDO.getId());
            a.setTitle(aDO.getTitle());
            a.setYear(aDO.getYear());
            a.setNica(aDO.getNica());
            List<CategoryDO> categories = aDO.getCategories();
            a.setCategories(categoryService.translate(categories));

            artworks.add(a);
        }
    }
	
     /**
     * @param artworkDAO Dependency Injection
     */
    public void setArtworkDAO(IArtworkDAO artworkDAO) {
        this.artworkDAO = artworkDAO;
    }
    
    /**
     * Dependency Injection
     * @param categoryService
     */
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

	@Override
	public void copyCoordinates(List<Artwork> artworks,
			List<ThemeLandscapeHexboardDataItem> hexCoordinates) {
		artworks.clear();
		for (ThemeLandscapeHexboardDataItem item : hexCoordinates) {
			Artwork aw = item.aw;
			aw.setX(item.getCol());
			aw.setY(item.getRow());
			artworks.add(aw);
		}
		
	}


	

}
